package logic

import (
	"class/classrpc/classrpc"
	"class/classrpc/internal/svc"
	"class/models"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type CourseAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCourseAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CourseAddLogic {
	return &CourseAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 课程发布
func (l *CourseAddLogic) CourseAdd(in *classrpc.CourseAddRequest) (*classrpc.CourseAddResponse, error) {
	// todo: add your logic here and delete this line

	if in.Title == "" || in.Image == "" || in.Info == "" || in.Category == "" || in.Price == 0 {
		return nil, fmt.Errorf("必填项不能为空")
	}

	var course models.Course
	models.DB.Debug().Where("title =?", in.Title).Find(&course)
	if course.ID != 0 {
		return nil, fmt.Errorf("当前课程已存在")
	}

	course = models.Course{
		Title:     in.Title,
		TeacherId: int(in.TeacherId),
		Category:  in.Category,
		Price:     float64(in.Price),
		Image:     in.Image,
		Info:      in.Info,
	}
	models.DB.Debug().Create(&course)

	return &classrpc.CourseAddResponse{
		CourseId: int64(course.ID),
	}, nil
}
